package com.bw.android.bwgao6week2a;

import androidx.lifecycle.ViewModelProvider;

import android.view.View;
import android.widget.Toast;

import com.bw.android.bwgao6week2a.databinding.ActivityRegisterBinding;
import com.bw.android.bwgao6week2a.vm.RegisterViewModel;
import com.bw.android.network.RequestClient;
import com.bw.android.network.bean.RegisterReqBean;
import com.bw.ch.baselib.BaseBindingActivity;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class RegisterActivity extends BaseBindingActivity<ActivityRegisterBinding> {
    private RegisterViewModel registerViewModel;

    @Override
    public int getLayout() {
        return R.layout.activity_register;
    }

    @Override
    protected void initView() {
        registerViewModel = new ViewModelProvider(this).get(RegisterViewModel.class);
        mBinding.setRegisterVm(registerViewModel);
        mBinding.btnClose.setOnClickListener(v -> {
            this.finish();
        });
        mBinding.btnRegister.setOnClickListener(this::onClick);
    }

    @Override
    protected void initData() {

    }

    private void onClick(View v) {
        if (registerViewModel.userName.getValue().trim().isEmpty()
                || registerViewModel.userName.getValue().trim().isEmpty()
                || registerViewModel.userName.getValue().trim().isEmpty()) {
            Toast.makeText(this, "用户名、昵称、密码 不可为空！", Toast.LENGTH_LONG).show();
        } else {
            RegisterReqBean reqBean = new RegisterReqBean();
            reqBean.userName = registerViewModel.userName.getValue();
            reqBean.nickName = registerViewModel.nickName.getValue();
            reqBean.passwd = registerViewModel.password.getValue();

            RequestClient.buildService().liveRegister(reqBean)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(loginBean -> {
                        Toast.makeText(this, loginBean.getStatus() + ", " + loginBean.getResult(), Toast.LENGTH_LONG).show();
                    }, throwable -> {
                        Toast.makeText(this, throwable.toString(), Toast.LENGTH_LONG).show();
                    });
        }
    }
}